// pages/socialSystem/articalDetail/articalDetail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        articalType: null,
        articalInfo: null,
        articalId: null,
        commentDetail: '',
        introGoods: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            articalId: options.articalId,
            articalType: options.articalType
        })
        this.getArticalInfo()
        this.getIntroGoods()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function(res) {
        let userId = wx.getStorageSync('userId')
        if (userId) {
            return {
                title: this.data.articalInfo.title,
                path: '/pages/socialSystem/articalDetail/articalDetail?articalId=' + this.data.articalId + '&articalType=' + this.data.articalType + '&userShare=' + userId,
            }
        }
    },

    getArticalInfo: function() {
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let data = {
            id: this.data.articalId,
            type: this.data.articalType,
            paramter: 1
        }
        common.post('Moments/moment_list', data).then(function(res) {
            wx.hideLoading()
            res[0].comment_list.reverse()
            that.setData({
                articalInfo: res[0]
            })
        }).catch(function(e) {})
    },

    getIntroGoods: function() {
        const that = this
        let data = {
            order_by: 'add_time',
            asc: 2,
            page: 0,
            limit: 20
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    introGoods: res
                })
            }
        }).catch(function(e) {})
    },

    commentChange: function(e) {
        this.setData({
            commentDetail: e.detail.value
        })
    },

    praiseClick: function() {
        const that = this
        let articalInfo = this.data.articalInfo
        if (articalInfo.is_like == 0) {
            let data = {
                moments_id: articalInfo.id
            }
            common.post('Moments/moment_like', data).then(function(res) {
                articalInfo.is_like = 1
                articalInfo.praise_num += 1
                that.setData({
                    articalInfo: articalInfo
                })
            }).catch(function(e) {})
        }
    },

    submitComment: function(e) {
        const that = this
        let data = {
            moments_id: this.data.articalInfo.id,
            content: this.data.commentDetail
        }
        common.post('Moments/moment_reply', data).then(function(res) {
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '评论成功，请耐心等待审核',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.getArticalInfo()
                        that.setData({
                            commentDetail: ''
                        })
                    }
                }
            })
        }).catch(function(e) {})
    }
})